home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 1843 / 1843.xpi / components / firebug-annotations.js next >
Text File  |  2010-01-15  |  6KB  |  231 lines

  1. /* See license.txt for terms of usage */
  2.  
  3. // ************************************************************************************************
  4. // Constants
  5.  
  6. const CLASS_ID = Components.ID("{9589DC0D-9709-4578-883E-D393452B3611}");
  7. const CLASS_NAME = "Firebug Annotation Service";
  8. const CONTRACT_ID = "@joehewitt.com/firebug-annotation-service;1";
  9.  
  10. const Cc = Components.classes;
  11. const Ci = Components.interfaces;
  12. const Cr = Components.results;
  13.  
  14. const dirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
  15.  
  16. // ************************************************************************************************
  17. // Annotaion service implementation
  18.  
  19. var FBTrace = null;
  20.  
  21. /**
  22.  * @class Represents an internal Firebug annotation service. This service is used to
  23.  * annotate sites with an info whether Firebug should be activated for them or not.
  24.  */
  25. function AnnotationService()
  26. {
  27.     this.wrappedJSObject = this;
  28.  
  29.     FBTrace = Cc["@joehewitt.com/firebug-trace-service;1"]
  30.        .getService(Ci.nsISupports).wrappedJSObject.getTracer("extensions.firebug");
  31.  
  32.     // Get annotation file stored within the profile directory.
  33.     this.file = dirService.get("ProfD", Ci.nsIFile);
  34.     this.file.append("firebug");
  35.     this.file.append("annotations.json");
  36.  
  37.     // Load annotaions.
  38.     this.initialize();
  39. }
  40.  
  41. AnnotationService.prototype =
  42. {
  43.     annotations: [],
  44.  
  45.     setPageAnnotation: function(uri, value)
  46.     {
  47.         this.annotations[uri.spec] = value;
  48.     },
  49.  
  50.     getPageAnnotation: function(uri)
  51.     {
  52.         return this.annotations[uri.spec];
  53.     },
  54.  
  55.     pageHasAnnotation: function(uri)
  56.     {
  57.         return this.annotations[uri.spec] ? true : false;
  58.     },
  59.  
  60.     removePageAnnotation: function(uri)
  61.     {
  62.         delete this.annotations[uri.spec];
  63.     },
  64.  
  65.     getAnnotations: function()
  66.     {
  67.         return this.annotations;
  68.     },
  69.  
  70.     clear: function()
  71.     {
  72.         this.annotations = [];
  73.     },
  74.  
  75.     // Persistence
  76.     initialize: function()
  77.     {
  78.         try
  79.         {
  80.             this.clear();
  81.  
  82.             if (!this.file.exists())
  83.             {
  84.                 this.file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
  85.                 return;
  86.             }
  87.  
  88.             var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
  89.                 .createInstance(Ci.nsIFileInputStream);
  90.             var cstream = Cc["@mozilla.org/intl/converter-input-stream;1"]
  91.                 .createInstance(Ci.nsIConverterInputStream);
  92.  
  93.             // Initialize input stream.
  94.             inputStream.init(this.file, 0x01 | 0x08, 0666, 0); // read, create
  95.             cstream.init(inputStream, "UTF-8", 0, 0);
  96.  
  97.             // Load annotations.
  98.             var data = {};
  99.             cstream.readString(-1, data);
  100.             if (!data.value.length)
  101.                 return;
  102.  
  103.             var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
  104.             var arr = nativeJSON.decode(data.value);
  105.             if (!arr)
  106.                 return;
  107.  
  108.             // Convert to map for faster lookup.
  109.             for (var i=0; i<arr.length; i++)
  110.                 this.annotations[arr[i].uri] = arr[i].value;
  111.  
  112.         }
  113.         catch (err)
  114.         {
  115.         }
  116.     },
  117.  
  118.     flush: function()
  119.     {
  120.         try
  121.         {
  122.             // Initialize output stream.
  123.             var outputStream = Cc["@mozilla.org/network/file-output-stream;1"]
  124.                 .createInstance(Ci.nsIFileOutputStream);
  125.             outputStream.init(this.file, 0x02 | 0x08 | 0x20, 0666, 0); // write, create, truncate
  126.  
  127.             // Convert data to JSON.
  128.             var arr = [];
  129.             for (var uri in this.annotations)
  130.                 arr.push({
  131.                     uri: uri,
  132.                     value: this.annotations[uri]
  133.                 });
  134.  
  135.             var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
  136.             var jsonString = nativeJSON.encode(arr);
  137.  
  138.             // Store annotations.
  139.             outputStream.write(jsonString, jsonString.length);
  140.             outputStream.close();
  141.  
  142.         }
  143.         catch (err)
  144.         {
  145.         }
  146.     },
  147.  
  148.     /* nsISupports */
  149.     QueryInterface: function(iid)
  150.     {
  151.         if (iid.equals(Ci.nsISupports))
  152.             return this;
  153.  
  154.         throw Cr.NS_ERROR_NO_INTERFACE;
  155.     }
  156. };
  157.  
  158. // ************************************************************************************************
  159. // Service factory
  160.  
  161. var gServiceSingleton = null;
  162. var AnnotationsFactory =
  163. {
  164.     createInstance: function (outer, iid)
  165.     {
  166.         if (outer != null)
  167.             throw Cr.NS_ERROR_NO_AGGREGATION;
  168.  
  169.         if (iid.equals(Ci.nsISupports))
  170.         {
  171.             if (!gServiceSingleton)
  172.                 gServiceSingleton = new AnnotationService();
  173.             return gServiceSingleton.QueryInterface(iid);
  174.         }
  175.  
  176.         throw Cr.NS_ERROR_NO_INTERFACE;
  177.     },
  178.  
  179.     QueryInterface: function(iid)
  180.     {
  181.         if (iid.equals(Ci.nsISupports) ||
  182.             iid.equals(Ci.nsISupportsWeakReference) ||
  183.             iid.equals(Ci.nsIFactory))
  184.             return this;
  185.  
  186.         throw Cr.NS_ERROR_NO_INTERFACE;
  187.     }
  188. };
  189.  
  190. // ************************************************************************************************
  191. // Module implementation
  192.  
  193. var AnnotationsModule =
  194. {
  195.     registerSelf: function (compMgr, fileSpec, location, type)
  196.     {
  197.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  198.         compMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME,
  199.             CONTRACT_ID, fileSpec, location, type);
  200.     },
  201.  
  202.     unregisterSelf: function(compMgr, fileSpec, location)
  203.     {
  204.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  205.         compMgr.unregisterFactoryLocation(CLASS_ID, location);
  206.     },
  207.  
  208.     getClassObject: function (compMgr, cid, iid)
  209.     {
  210.         if (!iid.equals(Ci.nsIFactory))
  211.             throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  212.  
  213.         if (cid.equals(CLASS_ID))
  214.             return AnnotationsFactory;
  215.  
  216.         throw Cr.NS_ERROR_NO_INTERFACE;
  217.     },
  218.  
  219.     canUnload: function(compMgr)
  220.     {
  221.         return true;
  222.     }
  223. };
  224.  
  225. // ************************************************************************************************
  226.  
  227. function NSGetModule(compMgr, fileSpec)
  228. {
  229.     return AnnotationsModule;
  230. }
  231.